

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cortex.built_ins.models.gan &mdash; Cortex2.0  documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 

  
  <script src="../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../index.html" class="icon icon-home"> Cortex2.0
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">User Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../modules.html">cortex</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../develop.html">Develop</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html">Custom demos</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#a-walkthrough-a-custom-classifier">A walkthrough a custom classifier:</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#defining-losses-and-results">Defining losses and results</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#visualization">Visualization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../build.html#putting-it-together">Putting it together</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">Cortex2.0</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>cortex.built_ins.models.gan</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cortex.built_ins.models.gan</h1><div class="highlight"><pre>
<span></span><span class="sd">&#39;&#39;&#39;Generative adversarial networks with various objectives and penalties.</span>

<span class="sd">&#39;&#39;&#39;</span>

<span class="kn">import</span> <span class="nn">math</span>

<span class="kn">from</span> <span class="nn">cortex.built_ins.networks.fully_connected</span> <span class="k">import</span> <span class="n">FullyConnectedNet</span>
<span class="kn">from</span> <span class="nn">cortex.plugins</span> <span class="k">import</span> <span class="n">register_plugin</span><span class="p">,</span> <span class="n">ModelPlugin</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">torch</span> <span class="k">import</span> <span class="n">autograd</span>
<span class="kn">import</span> <span class="nn">torch.nn.functional</span> <span class="k">as</span> <span class="nn">F</span>

<span class="kn">from</span> <span class="nn">.utils</span> <span class="k">import</span> <span class="n">log_sum_exp</span><span class="p">,</span> <span class="n">update_decoder_args</span><span class="p">,</span> <span class="n">update_encoder_args</span>


<div class="viewcode-block" id="raise_measure_error"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.raise_measure_error">[docs]</a><span class="k">def</span> <span class="nf">raise_measure_error</span><span class="p">(</span><span class="n">measure</span><span class="p">):</span>
    <span class="n">supported_measures</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;GAN&#39;</span><span class="p">,</span> <span class="s1">&#39;JSD&#39;</span><span class="p">,</span> <span class="s1">&#39;X2&#39;</span><span class="p">,</span> <span class="s1">&#39;KL&#39;</span><span class="p">,</span> <span class="s1">&#39;RKL&#39;</span><span class="p">,</span> <span class="s1">&#39;DV&#39;</span><span class="p">,</span> <span class="s1">&#39;H2&#39;</span><span class="p">,</span> <span class="s1">&#39;W1&#39;</span><span class="p">]</span>
    <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
        <span class="s1">&#39;Measure `</span><span class="si">{}</span><span class="s1">` not supported. Supported: </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">measure</span><span class="p">,</span>
                                                           <span class="n">supported_measures</span><span class="p">))</span></div>


<div class="viewcode-block" id="get_positive_expectation"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.get_positive_expectation">[docs]</a><span class="k">def</span> <span class="nf">get_positive_expectation</span><span class="p">(</span><span class="n">p_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="n">log_2</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mf">2.</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;GAN&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="o">-</span> <span class="n">F</span><span class="o">.</span><span class="n">softplus</span><span class="p">(</span><span class="o">-</span><span class="n">p_samples</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;JSD&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="n">log_2</span> <span class="o">-</span> <span class="n">F</span><span class="o">.</span><span class="n">softplus</span><span class="p">(</span><span class="o">-</span> <span class="n">p_samples</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;X2&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="n">p_samples</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;KL&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="n">p_samples</span> <span class="o">+</span> <span class="mf">1.</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;RKL&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="o">-</span><span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">p_samples</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;DV&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="n">p_samples</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;H2&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="mf">1.</span> <span class="o">-</span> <span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">p_samples</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;W1&#39;</span><span class="p">:</span>
        <span class="n">Ep</span> <span class="o">=</span> <span class="n">p_samples</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">raise_measure_error</span><span class="p">(</span><span class="n">measure</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">average</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Ep</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Ep</span></div>


<div class="viewcode-block" id="get_negative_expectation"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.get_negative_expectation">[docs]</a><span class="k">def</span> <span class="nf">get_negative_expectation</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">,</span> <span class="n">average</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="n">log_2</span> <span class="o">=</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mf">2.</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;GAN&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">softplus</span><span class="p">(</span><span class="o">-</span><span class="n">q_samples</span><span class="p">)</span> <span class="o">+</span> <span class="n">q_samples</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;JSD&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">F</span><span class="o">.</span><span class="n">softplus</span><span class="p">(</span><span class="o">-</span><span class="n">q_samples</span><span class="p">)</span> <span class="o">+</span> <span class="n">q_samples</span> <span class="o">-</span> <span class="n">log_2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;X2&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="p">((</span><span class="n">torch</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">q_samples</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;KL&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">q_samples</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;RKL&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">q_samples</span> <span class="o">-</span> <span class="mf">1.</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;DV&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">log_sum_exp</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-</span> <span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">q_samples</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;H2&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">q_samples</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1.</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;W1&#39;</span><span class="p">:</span>
        <span class="n">Eq</span> <span class="o">=</span> <span class="n">q_samples</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">raise_measure_error</span><span class="p">(</span><span class="n">measure</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">average</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Eq</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Eq</span></div>


<div class="viewcode-block" id="get_boundary"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.get_boundary">[docs]</a><span class="k">def</span> <span class="nf">get_boundary</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">measure</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;GAN&#39;</span><span class="p">,</span> <span class="s1">&#39;JSD&#39;</span><span class="p">,</span> <span class="s1">&#39;KL&#39;</span><span class="p">,</span> <span class="s1">&#39;RKL&#39;</span><span class="p">,</span> <span class="s1">&#39;H2&#39;</span><span class="p">,</span> <span class="s1">&#39;DV&#39;</span><span class="p">):</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">samples</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;X2&#39;</span><span class="p">:</span>
        <span class="n">b</span> <span class="o">=</span> <span class="p">(</span><span class="n">samples</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;W&#39;</span><span class="p">:</span>
        <span class="n">b</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">raise_measure_error</span><span class="p">(</span><span class="n">measure</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">b</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span></div>


<div class="viewcode-block" id="get_weight"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.get_weight">[docs]</a><span class="k">def</span> <span class="nf">get_weight</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">measure</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;GAN&#39;</span><span class="p">,</span> <span class="s1">&#39;JSD&#39;</span><span class="p">,</span> <span class="s1">&#39;KL&#39;</span><span class="p">,</span> <span class="s1">&#39;RKL&#39;</span><span class="p">,</span> <span class="s1">&#39;DV&#39;</span><span class="p">,</span> <span class="s1">&#39;H2&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">samples</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;X2&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">samples</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="k">elif</span> <span class="n">measure</span> <span class="o">==</span> <span class="s1">&#39;W1&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">raise_measure_error</span><span class="p">(</span><span class="n">measure</span><span class="p">)</span></div>


<div class="viewcode-block" id="generator_loss"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.generator_loss">[docs]</a><span class="k">def</span> <span class="nf">generator_loss</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">,</span> <span class="n">loss_type</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">loss_type</span> <span class="ow">or</span> <span class="n">loss_type</span> <span class="o">==</span> <span class="s1">&#39;minimax&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">get_negative_expectation</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">loss_type</span> <span class="o">==</span> <span class="s1">&#39;non-saturating&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="o">-</span><span class="n">get_positive_expectation</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">loss_type</span> <span class="o">==</span> <span class="s1">&#39;boundary-seek&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">get_boundary</span><span class="p">(</span><span class="n">q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s1">&#39;Generator loss type `</span><span class="si">{}</span><span class="s1">` not supported. &#39;</span>
            <span class="s1">&#39;Supported: [None, non-saturating, boundary-seek]&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="GradientPenalty"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GradientPenalty">[docs]</a><span class="k">class</span> <span class="nc">GradientPenalty</span><span class="p">(</span><span class="n">ModelPlugin</span><span class="p">):</span>

<div class="viewcode-block" id="GradientPenalty.routine"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GradientPenalty.routine">[docs]</a>    <span class="k">def</span> <span class="nf">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">penalty_type</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;contractive&#39;</span><span class="p">,</span>
                <span class="n">penalty_amount</span><span class="p">:</span> <span class="nb">float</span><span class="o">=</span><span class="mf">0.5</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            penalty_type: Gradient penalty type for the discriminator.</span>
<span class="sd">                {contractive}</span>
<span class="sd">            penalty_amount: Amount of gradient penalty for the discriminator.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">penalty_type</span> <span class="o">==</span> <span class="s1">&#39;contractive&#39;</span><span class="p">:</span>
            <span class="n">penalty</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">contractive_penalty</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">network</span><span class="p">,</span> <span class="n">inputs</span><span class="p">,</span> <span class="n">penalty_amount</span><span class="o">=</span><span class="n">penalty_amount</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="n">penalty_type</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">penalty</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">network</span> <span class="o">=</span> <span class="n">penalty</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">penalty_type</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="s1">&#39;penalty&#39;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">penalty</span><span class="o">.</span><span class="n">item</span><span class="p">()</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_get_gradient</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="n">output</span><span class="p">):</span>
        <span class="n">gradient</span> <span class="o">=</span> <span class="n">autograd</span><span class="o">.</span><span class="n">grad</span><span class="p">(</span><span class="n">outputs</span><span class="o">=</span><span class="n">output</span><span class="p">,</span> <span class="n">inputs</span><span class="o">=</span><span class="n">inp</span><span class="p">,</span>
                                 <span class="n">grad_outputs</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">ones_like</span><span class="p">(</span><span class="n">output</span><span class="p">),</span>
                                 <span class="n">create_graph</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">retain_graph</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                 <span class="n">only_inputs</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">allow_unused</span><span class="o">=</span><span class="kc">True</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">gradient</span>

<div class="viewcode-block" id="GradientPenalty.contractive_penalty"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GradientPenalty.contractive_penalty">[docs]</a>    <span class="k">def</span> <span class="nf">contractive_penalty</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">penalty_amount</span><span class="o">=</span><span class="mf">0.5</span><span class="p">):</span>

        <span class="k">if</span> <span class="n">penalty_amount</span> <span class="o">==</span> <span class="mf">0.</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="nb">input</span> <span class="o">=</span> <span class="p">[</span><span class="nb">input</span><span class="p">]</span>

        <span class="nb">input</span> <span class="o">=</span> <span class="p">[</span><span class="n">inp</span><span class="o">.</span><span class="n">detach</span><span class="p">()</span> <span class="k">for</span> <span class="n">inp</span> <span class="ow">in</span> <span class="nb">input</span><span class="p">]</span>
        <span class="nb">input</span> <span class="o">=</span> <span class="p">[</span><span class="n">inp</span><span class="o">.</span><span class="n">requires_grad_</span><span class="p">()</span> <span class="k">for</span> <span class="n">inp</span> <span class="ow">in</span> <span class="nb">input</span><span class="p">]</span>

        <span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">set_grad_enabled</span><span class="p">(</span><span class="kc">True</span><span class="p">):</span>
            <span class="n">output</span> <span class="o">=</span> <span class="n">network</span><span class="p">(</span><span class="o">*</span><span class="nb">input</span><span class="p">)</span>
        <span class="n">gradient</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_gradient</span><span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span>
        <span class="n">gradient</span> <span class="o">=</span> <span class="n">gradient</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">gradient</span><span class="o">.</span><span class="n">size</span><span class="p">()[</span><span class="mi">0</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">penalty</span> <span class="o">=</span> <span class="p">(</span><span class="n">gradient</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">penalty_amount</span> <span class="o">*</span> <span class="n">penalty</span></div>

<div class="viewcode-block" id="GradientPenalty.interpolate_penalty"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GradientPenalty.interpolate_penalty">[docs]</a>    <span class="k">def</span> <span class="nf">interpolate_penalty</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">,</span> <span class="nb">input</span><span class="p">,</span> <span class="n">penalty_amount</span><span class="o">=</span><span class="mf">0.5</span><span class="p">):</span>

        <span class="nb">input</span> <span class="o">=</span> <span class="nb">input</span><span class="o">.</span><span class="n">detach</span><span class="p">()</span>
        <span class="nb">input</span> <span class="o">=</span> <span class="nb">input</span><span class="o">.</span><span class="n">requires_grad_</span><span class="p">()</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">input</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;tuple of 2 inputs required to interpolate&#39;</span><span class="p">)</span>
        <span class="n">inp1</span><span class="p">,</span> <span class="n">inp2</span> <span class="o">=</span> <span class="nb">input</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">epsilon</span> <span class="o">=</span> <span class="n">network</span><span class="o">.</span><span class="n">inputs</span><span class="o">.</span><span class="n">e</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;You must initiate a uniform random variable&#39;</span>
                             <span class="s1">&#39;`e` to use interpolation&#39;</span><span class="p">)</span>
        <span class="n">mid_in</span> <span class="o">=</span> <span class="p">((</span><span class="mf">1.</span> <span class="o">-</span> <span class="n">epsilon</span><span class="p">)</span> <span class="o">*</span> <span class="n">inp1</span> <span class="o">+</span> <span class="n">epsilon</span> <span class="o">*</span> <span class="n">inp2</span><span class="p">)</span>
        <span class="n">mid_in</span><span class="o">.</span><span class="n">requires_grad_</span><span class="p">()</span>

        <span class="k">with</span> <span class="n">torch</span><span class="o">.</span><span class="n">set_grad_enabled</span><span class="p">(</span><span class="kc">True</span><span class="p">):</span>
            <span class="n">mid_out</span> <span class="o">=</span> <span class="n">network</span><span class="p">(</span><span class="n">mid_in</span><span class="p">)</span>
        <span class="n">gradient</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_gradient</span><span class="p">(</span><span class="n">mid_in</span><span class="p">,</span> <span class="n">mid_out</span><span class="p">)</span>
        <span class="n">gradient</span> <span class="o">=</span> <span class="n">gradient</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">gradient</span><span class="o">.</span><span class="n">size</span><span class="p">()[</span><span class="mi">0</span><span class="p">],</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">penalty</span> <span class="o">=</span> <span class="p">((</span><span class="n">gradient</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">dim</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">-</span> <span class="mf">1.</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">penalty_amount</span> <span class="o">*</span> <span class="n">penalty</span></div></div>


<div class="viewcode-block" id="Discriminator"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Discriminator">[docs]</a><span class="k">class</span> <span class="nc">Discriminator</span><span class="p">(</span><span class="n">ModelPlugin</span><span class="p">):</span>

<div class="viewcode-block" id="Discriminator.build"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Discriminator.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">discriminator_type</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;convnet&#39;</span><span class="p">,</span> <span class="n">discriminator_args</span><span class="o">=</span><span class="p">{}):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            discriminator_type: Discriminator network type.</span>
<span class="sd">            discriminator_args: Discriminator network arguments.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x_shape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dims</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
        <span class="n">Encoder</span><span class="p">,</span> <span class="n">discriminator_args</span> <span class="o">=</span> <span class="n">update_encoder_args</span><span class="p">(</span>
            <span class="n">x_shape</span><span class="p">,</span> <span class="n">model_type</span><span class="o">=</span><span class="n">discriminator_type</span><span class="p">,</span>
            <span class="n">encoder_args</span><span class="o">=</span><span class="n">discriminator_args</span><span class="p">)</span>
        <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Encoder</span><span class="p">(</span><span class="n">x_shape</span><span class="p">,</span> <span class="n">dim_out</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">discriminator_args</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="n">discriminator</span></div>

<div class="viewcode-block" id="Discriminator.routine"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Discriminator.routine">[docs]</a>    <span class="k">def</span> <span class="nf">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">fake</span><span class="p">,</span> <span class="n">measure</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;GAN&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            measure: GAN measure.</span>
<span class="sd">                {GAN, JSD, KL, RKL (reverse KL), X2 (Chi^2), H2 (squared</span>
<span class="sd">                Hellinger), DV (Donsker Varahdan KL), W1 (IPM)}</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">X_P</span> <span class="o">=</span> <span class="n">real</span>
        <span class="n">X_Q</span> <span class="o">=</span> <span class="n">fake</span>
        <span class="n">E_pos</span><span class="p">,</span> <span class="n">E_neg</span><span class="p">,</span> <span class="n">P_samples</span><span class="p">,</span> <span class="n">Q_samples</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X_P</span><span class="p">,</span> <span class="n">X_Q</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>

        <span class="n">difference</span> <span class="o">=</span> <span class="n">E_pos</span> <span class="o">-</span> <span class="n">E_neg</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">Scores</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">Ep</span><span class="o">=</span><span class="n">P_samples</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">.</span><span class="n">item</span><span class="p">(),</span>
                                        <span class="n">Eq</span><span class="o">=</span><span class="n">Q_samples</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">.</span><span class="n">item</span><span class="p">()))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="p">[</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1"> distance&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">measure</span><span class="p">)]</span> <span class="o">=</span> <span class="n">difference</span><span class="o">.</span><span class="n">item</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="o">-</span><span class="n">difference</span></div>

<div class="viewcode-block" id="Discriminator.score"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Discriminator.score">[docs]</a>    <span class="k">def</span> <span class="nf">score</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X_P</span><span class="p">,</span> <span class="n">X_Q</span><span class="p">,</span> <span class="n">measure</span><span class="p">):</span>
        <span class="n">discriminator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">discriminator</span>
        <span class="n">P_samples</span> <span class="o">=</span> <span class="n">discriminator</span><span class="p">(</span><span class="n">X_P</span><span class="p">)</span>
        <span class="n">Q_samples</span> <span class="o">=</span> <span class="n">discriminator</span><span class="p">(</span><span class="n">X_Q</span><span class="p">)</span>

        <span class="n">E_pos</span> <span class="o">=</span> <span class="n">get_positive_expectation</span><span class="p">(</span><span class="n">P_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>
        <span class="n">E_neg</span> <span class="o">=</span> <span class="n">get_negative_expectation</span><span class="p">(</span><span class="n">Q_samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">E_pos</span><span class="p">,</span> <span class="n">E_neg</span><span class="p">,</span> <span class="n">P_samples</span><span class="p">,</span> <span class="n">Q_samples</span></div>

<div class="viewcode-block" id="Discriminator.visualize"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Discriminator.visualize">[docs]</a>    <span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">real</span><span class="p">,</span> <span class="n">fake</span><span class="p">,</span> <span class="n">measure</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">X_P</span> <span class="o">=</span> <span class="n">real</span>
        <span class="n">X_Q</span> <span class="o">=</span> <span class="n">fake</span>

        <span class="n">E_pos</span><span class="p">,</span> <span class="n">E_neg</span><span class="p">,</span> <span class="n">P_samples</span><span class="p">,</span> <span class="n">Q_samples</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">score</span><span class="p">(</span><span class="n">X_P</span><span class="p">,</span> <span class="n">X_Q</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_histogram</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">fake</span><span class="o">=</span><span class="n">Q_samples</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">data</span><span class="p">,</span>
                                <span class="n">real</span><span class="o">=</span><span class="n">P_samples</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">data</span><span class="p">),</span>
                           <span class="n">name</span><span class="o">=</span><span class="s1">&#39;discriminator output&#39;</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="SimpleDiscriminator"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.SimpleDiscriminator">[docs]</a><span class="k">class</span> <span class="nc">SimpleDiscriminator</span><span class="p">(</span><span class="n">Discriminator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Discriminator for 1d vectors.</span>

<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="SimpleDiscriminator.build"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.SimpleDiscriminator.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim_in</span><span class="p">:</span> <span class="nb">int</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">discriminator_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">dim_h</span><span class="o">=</span><span class="p">[</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">])):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            dim_in (int): Input size</span>
<span class="sd">            dim_out (int): Output size</span>
<span class="sd">            classifier_args: Extra arguments for building the classifier</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">discriminator</span> <span class="o">=</span> <span class="n">FullyConnectedNet</span><span class="p">(</span><span class="n">dim_in</span><span class="p">,</span> <span class="n">dim_out</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                                          <span class="o">**</span><span class="n">discriminator_args</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="n">discriminator</span></div></div>


<div class="viewcode-block" id="Generator"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Generator">[docs]</a><span class="k">class</span> <span class="nc">Generator</span><span class="p">(</span><span class="n">ModelPlugin</span><span class="p">):</span>

<div class="viewcode-block" id="Generator.build"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Generator.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dim_z</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">generator_type</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;convnet&#39;</span><span class="p">,</span>
              <span class="n">generator_args</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">output_nonlinearity</span><span class="o">=</span><span class="s1">&#39;tanh&#39;</span><span class="p">)):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            generator_noise_type: Type of input noise for the generator.</span>
<span class="sd">            dim_z: Input noise dimension for generator.</span>
<span class="sd">            generator_type: Generator network type.</span>
<span class="sd">            generator_args: Generator network arguments.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">x_shape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_dims</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>

        <span class="n">Decoder</span><span class="p">,</span> <span class="n">generator_args</span> <span class="o">=</span> <span class="n">update_decoder_args</span><span class="p">(</span>
            <span class="n">x_shape</span><span class="p">,</span> <span class="n">model_type</span><span class="o">=</span><span class="n">generator_type</span><span class="p">,</span> <span class="n">decoder_args</span><span class="o">=</span><span class="n">generator_args</span><span class="p">)</span>
        <span class="n">generator</span> <span class="o">=</span> <span class="n">Decoder</span><span class="p">(</span><span class="n">x_shape</span><span class="p">,</span> <span class="n">dim_in</span><span class="o">=</span><span class="n">dim_z</span><span class="p">,</span> <span class="o">**</span><span class="n">generator_args</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">generator</span> <span class="o">=</span> <span class="n">generator</span></div>

<div class="viewcode-block" id="Generator.routine"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Generator.routine">[docs]</a>    <span class="k">def</span> <span class="nf">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Z</span><span class="p">,</span> <span class="n">measure</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">loss_type</span><span class="p">:</span> <span class="nb">str</span><span class="o">=</span><span class="s1">&#39;non-saturating&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            loss_type: Generator loss type.</span>
<span class="sd">                {non-saturating, minimax, boundary-seek}</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">discriminator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">discriminator</span>
        <span class="n">generator</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">generator</span>

        <span class="n">X_Q</span> <span class="o">=</span> <span class="n">generator</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
        <span class="n">samples</span> <span class="o">=</span> <span class="n">discriminator</span><span class="p">(</span><span class="n">X_Q</span><span class="p">)</span>

        <span class="n">g_loss</span> <span class="o">=</span> <span class="n">generator_loss</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">,</span> <span class="n">loss_type</span><span class="o">=</span><span class="n">loss_type</span><span class="p">)</span>
        <span class="n">weights</span> <span class="o">=</span> <span class="n">get_weight</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">measure</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">generator</span> <span class="o">=</span> <span class="n">g_loss</span>
        <span class="k">if</span> <span class="n">weights</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">results</span><span class="o">.</span><span class="n">Weights</span> <span class="o">=</span> <span class="n">weights</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">.</span><span class="n">item</span><span class="p">()</span></div>

<div class="viewcode-block" id="Generator.generate"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Generator.generate">[docs]</a>    <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Z</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">generator</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span></div>

<div class="viewcode-block" id="Generator.visualize"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.Generator.visualize">[docs]</a>    <span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">Z</span><span class="p">):</span>
        <span class="n">X_Q</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="n">X_Q</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;generated&#39;</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="GAN"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GAN">[docs]</a><span class="k">class</span> <span class="nc">GAN</span><span class="p">(</span><span class="n">ModelPlugin</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generative adversarial network.</span>
<span class="sd">        A generative adversarial network on images.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">defaults</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
        <span class="n">data</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">batch_size</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">train</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">test</span><span class="o">=</span><span class="mi">64</span><span class="p">),</span>
                  <span class="n">inputs</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">inputs</span><span class="o">=</span><span class="s1">&#39;images&#39;</span><span class="p">)),</span>
        <span class="n">train</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">save_on_lowest</span><span class="o">=</span><span class="s1">&#39;losses.gan&#39;</span><span class="p">)</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span> <span class="o">=</span> <span class="n">Discriminator</span><span class="p">()</span>
        <span class="n">penalty_contract</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">nets</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">network</span><span class="o">=</span><span class="s1">&#39;discriminator&#39;</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span> <span class="o">=</span> <span class="n">GradientPenalty</span><span class="p">(</span><span class="n">contract</span><span class="o">=</span><span class="n">penalty_contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span> <span class="o">=</span> <span class="n">Generator</span><span class="p">()</span>

<div class="viewcode-block" id="GAN.build"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GAN.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">noise_type</span><span class="o">=</span><span class="s1">&#39;normal&#39;</span><span class="p">,</span> <span class="n">dim_z</span><span class="o">=</span><span class="mi">64</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            noise_type: Distribution of input noise for generator.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_noise</span><span class="p">(</span><span class="s1">&#39;Z&#39;</span><span class="p">,</span> <span class="n">dist</span><span class="o">=</span><span class="n">noise_type</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">dim_z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_noise</span><span class="p">(</span><span class="s1">&#39;E&#39;</span><span class="p">,</span> <span class="n">dist</span><span class="o">=</span><span class="s1">&#39;uniform&#39;</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">build</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">build</span><span class="p">()</span></div>

<div class="viewcode-block" id="GAN.train_step"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GAN.train_step">[docs]</a>    <span class="k">def</span> <span class="nf">train_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">generator_updates</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">discriminator_updates</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        Args:</span>
<span class="sd">            generator_updates: Number of generator updates per step.</span>
<span class="sd">            discriminator_updates: Number of discriminator updates per step.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">discriminator_updates</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span> <span class="s1">&#39;Z&#39;</span><span class="p">)</span>
            <span class="n">generated</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">generated</span><span class="o">.</span><span class="n">detach</span><span class="p">())</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">optimizer_step</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span><span class="o">.</span><span class="n">train_step</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">generator_updates</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">train_step</span><span class="p">()</span></div>

<div class="viewcode-block" id="GAN.eval_step"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GAN.eval_step">[docs]</a>    <span class="k">def</span> <span class="nf">eval_step</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>

        <span class="n">inputs</span><span class="p">,</span> <span class="n">Z</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">inputs</span><span class="p">(</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span> <span class="s1">&#39;Z&#39;</span><span class="p">)</span>
        <span class="n">generated</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">inputs</span><span class="p">,</span> <span class="n">generated</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">penalty</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="GAN.visualize"><a class="viewcode-back" href="../../../../cortex.built_ins.models.html#cortex.built_ins.models.gan.GAN.visualize">[docs]</a>    <span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">images</span><span class="p">,</span> <span class="n">Z</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="n">images</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;ground truth&#39;</span><span class="p">)</span>
        <span class="n">generated</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">discriminator</span><span class="o">.</span><span class="n">visualize</span><span class="p">(</span><span class="n">images</span><span class="p">,</span> <span class="n">generated</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">generator</span><span class="o">.</span><span class="n">visualize</span><span class="p">(</span><span class="n">Z</span><span class="p">)</span></div></div>


<span class="n">register_plugin</span><span class="p">(</span><span class="n">GAN</span><span class="p">)</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, MILA.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../',
            VERSION:'',
            LANGUAGE:'None',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>